package util;

import hw.Users;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AtmApi {
    public static Connection getConnection() {
        Connection con = null;
        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 创建连接

            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/hw?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "kevind110");
            System.out.println("con:" + con);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return con;
    }

    //添加
    public static void inset(int num ,int u_id) {
        Connection con = getConnection();
        Statement statement = null;
        try {
            statement = con.createStatement();
            String sql = "UPDATE t_user set u_balance =" + num +" WHERE u_id =" + u_id;
            int row = statement.executeUpdate(sql);
            System.out.println(row);
        } catch (SQLException exception) {
            exception.printStackTrace();
        } finally {
            close(con, statement);
        }
    }
    public static void changpw (String str ,int u_id) {
        Connection con = getConnection();
        Statement statement = null;
        try {
            statement = con.createStatement();
            String sql = "UPDATE t_user set u_password =" + str +" WHERE u_id =" + u_id;
            int row = statement.executeUpdate(sql);
            System.out.println(row);
        } catch (SQLException exception) {
            exception.printStackTrace();
        } finally {
            close(con, statement);
        }
    }

    public static List query() {
        Connection con = getConnection();
        Statement statement = null;
        ResultSet rs = null;
        List userList = new ArrayList();
        try {
            statement = con.createStatement();
            String sql = "select * from t_user";
            rs = statement.executeQuery(sql);
            while (rs.next()) {
                Users users = new Users();
                users.setId(rs.getInt("u_id"));
                users.setBalance(rs.getInt("u_balance"));
                users.setName(rs.getString("u_name"));
                users.setPassword(rs.getString("u_password"));
                userList.add(users);
            }
        } catch (SQLException exception) {
            exception.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return userList;
    }


    public static void close(Connection con, Statement statement) {
        if (statement != null) {
            try {
                statement.close();
                con.close();
            } catch (SQLException exception) {
                exception.printStackTrace();
            }
        }
    }

    public static void close(Connection con, Statement statement, ResultSet rs) {
        if (statement != null) {
            try {
                rs.close();
                statement.close();
                con.close();
            } catch (SQLException exception) {
                exception.printStackTrace();
            }
        }
    }

    static String s;

    public static void main(String[] args) {
        System.out.println(AtmApi.query().get(1));

    }
}
